package cn.kwq.project_manger.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.kwq.project_manger.common.R;
import cn.kwq.project_manger.common.RES;
import cn.kwq.project_manger.pojo.User;
import cn.kwq.project_manger.pojo.dto.LoginDto;
import cn.kwq.project_manger.service.LoginService;

import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags ="登录服务")
@RestController
@RequestMapping("/login/")
public class LoginController {

    @Autowired
    LoginService loginService;


    @ApiOperation("登录接口")
    @SaIgnore//可以匿名访问该接口
    @PostMapping("loginWyuSys")
    public RES Login(@RequestBody LoginDto loginDto) {
        try {
            User user = loginService.loginByWYU(loginDto.getToken(),loginDto.getCode());
            //建立会话
            StpUtil.login(user.getId());
            log.info(user.getUserName()+"登录成功");
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            // DONE: 2022/11/6 token返回前端
            return R.ok("登陆成功",tokenInfo);
        }catch (Exception e){
            return R.err(500,e.getMessage());
        }
    }






}
